package com.modern.devtools.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MainController implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @FXML
    private StackPane contentPane;

    @FXML
    private Label statusLabel;

    @FXML
    private Label connectionStatus;

    // Navigation buttons
    @FXML
    private Button dashboardBtn;

    @FXML
    private Button serverManagementBtn;

    @FXML
    private Button monitoringBtn;

    @FXML
    private Button deploymentBtn;

    @FXML
    private Button scriptLibraryBtn;

    @FXML
    private Button settingsBtn;

    // Menu items
    @FXML
    private javafx.scene.control.MenuItem settingsMenuItem;

    @FXML
    private javafx.scene.control.MenuItem exitMenuItem;

    @FXML
    private javafx.scene.control.MenuItem aboutMenuItem;

    @FXML
    private void initialize() {
        // Initialize the controller
        updateConnectionStatus();
        // Load default dashboard view
        loadDashboardView();
    }

    @FXML
    protected void onDashboardClick() {
        loadDashboardView();
    }

    @FXML
    protected void onServerManagementClick() {
        loadServerManagementView();
    }

    @FXML
    protected void onMonitoringClick() {
        loadMonitoringView();
    }

    @FXML
    protected void onDeploymentClick() {
        loadDeploymentView();
    }

    @FXML
    protected void onScriptLibraryClick() {
        loadScriptLibraryView();
    }

    @FXML
    protected void onSettingsClick() {
        loadSettingsView();
    }

    private void loadDashboardView() {
        statusLabel.setText("Dashboard view loaded");
        contentPane.getChildren().clear();

        // Create a simple dashboard
        javafx.scene.layout.VBox dashboard = new javafx.scene.layout.VBox();
        dashboard.setSpacing(20);
        dashboard.setStyle("-fx-padding: 50; -fx-alignment: center;");

        javafx.scene.control.Label titleLabel = new javafx.scene.control.Label("Stardust Ops Dashboard");
        titleLabel.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");

        javafx.scene.control.Label subtitleLabel = new javafx.scene.control.Label("Welcome to the Modern Server Management Platform");
        subtitleLabel.setStyle("-fx-font-size: 14px;");

        dashboard.getChildren().addAll(titleLabel, subtitleLabel);
        contentPane.getChildren().add(dashboard);
    }

    private void loadServerManagementView() {
        try {
            statusLabel.setText("Server Management view loaded");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/views/server-management.fxml"));
            // Set the Spring context so that @Autowired works in the view controller
            loader.setControllerFactory(applicationContext::getBean);
            Parent root = loader.load();
            contentPane.getChildren().clear();
            contentPane.getChildren().add(root);
        } catch (IOException e) {
            statusLabel.setText("Error loading Server Management view: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void loadMonitoringView() {
        try {
            statusLabel.setText("Monitoring view loaded");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/views/monitoring.fxml"));
            loader.setControllerFactory(applicationContext::getBean);
            Parent root = loader.load();
            contentPane.getChildren().clear();
            contentPane.getChildren().add(root);
        } catch (IOException e) {
            statusLabel.setText("Error loading Monitoring view: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void loadDeploymentView() {
        try {
            statusLabel.setText("Deployment view loaded");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/views/deployment.fxml"));
            loader.setControllerFactory(applicationContext::getBean);
            Parent root = loader.load();
            contentPane.getChildren().clear();
            contentPane.getChildren().add(root);
        } catch (IOException e) {
            statusLabel.setText("Error loading Deployment view: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void loadScriptLibraryView() {
        try {
            statusLabel.setText("Script Library view loaded");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/views/script-library.fxml"));
            loader.setControllerFactory(applicationContext::getBean);
            Parent root = loader.load();
            contentPane.getChildren().clear();
            contentPane.getChildren().add(root);
        } catch (IOException e) {
            statusLabel.setText("Error loading Script Library view: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void loadSettingsView() {
        try {
            statusLabel.setText("Settings view loaded");
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/views/settings.fxml"));
            loader.setControllerFactory(applicationContext::getBean);
            Parent root = loader.load();
            contentPane.getChildren().clear();
            contentPane.getChildren().add(root);
        } catch (IOException e) {
            statusLabel.setText("Error loading Settings view: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void updateConnectionStatus() {
        // For now, just set a mock status
        connectionStatus.setText("Connected: 0 servers");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
